﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MinistryStandardPlatformEntity;

namespace MinistryStandardPlatformWeb.Controllers
{
    public class PresettingController : Controller
    {
        //
        // GET: /Presetting/

        private MSPDataContext context = new MSPDataContext();

        public ActionResult Index()
        {
            return PartialView();
        }

        public ActionResult Name(string id)
        {
            MenuPreferences model1 = context.MenuPreferences.First(u => u.Userid == id);
            return View(model1);
        }

        [HttpPost]
       public ActionResult Name(FormCollection form)
       {
           if (ModelState.IsValid)
           {
               var unitToUpdate = context.MenuPreferences.SingleOrDefault(u => u.Userid == form["Userid"]);
               try
               {
                   if (TryUpdateModel(unitToUpdate, form.ToValueProvider()))
                   {
                       context.SubmitChanges();
                       ViewBag.Message = "修改成功";
                   }
                   else
                   {
                       ViewBag.Message = "修改失败";
                   }
               }
               catch
               {
                   ViewBag.Message = "修改失败";
               }
           }
           else
           {
               ViewBag.Message = "修改失败";
           }
           return View("ActionResult");
       }


        public ActionResult Logo(string id)
        {
            MenuPreferences model1 = context.MenuPreferences.First(u => u.Userid == id);
            return View(model1);
        }

        [HttpPost]
        public ActionResult Logo(HttpPostedFileBase imageUpLoad, FormCollection form)
        {
            var logoToUpdate = context.MenuPreferences.SingleOrDefault(u => u.Userid == form["Userid"]);
            string fileName = imageUpLoad.FileName; 
            //转换只取得文件名，去掉路径。 
            if (fileName.LastIndexOf("\\") > -1) 
            { 
            fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1); 
            } 
            //保存到相对路径下。 
            imageUpLoad.SaveAs(Server.MapPath("~/Upload/SystemSetting/" + fileName)); 
            //以下代码是将 路径保存到数据库。 
            
            string ImagePath = "~/Upload/SystemSetting/" + fileName;
            logoToUpdate.LogoPath=ImagePath;
            try
            {
                if (TryUpdateModel(logoToUpdate))
                {
                    context.SubmitChanges();
                    ViewBag.Message = "修改成功";
                }
                else
                {
                    ViewBag.Message = "修改失败";
                }
            }
            catch
            {
                ViewBag.Message = "修改失败";
            }

            return View("ActionResult");
        }
    }
}
